package com.own.demo.长沙空中路网格子;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.DateUtils;
import com.own.demo.writerfile.GeoJsonToOsmXML;
import com.own.demo.writerfile.WriterFileUtils;
import com.own.demo.长沙空中路网格子.Thread.LonLat6Thread;
import com.own.demo.长沙空中路网格子.Thread.PointLatThread;
import com.own.demo.长沙空中路网格子.Thread.PointLonThread;
import lombok.SneakyThrows;
import org.locationtech.jts.geom.Coordinate;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class 添加路网ALL extends Parent {

    private static String geoOSMUrl = "E:\\ownCode\\UMA\\uam\\uam-air-engine";
    private static String geoOSMName = "\\长沙市1-base-V5.geojson.osm";

    private static String geoJson = "E:\\ownCode\\UMA\\数据\\路网数据\\base";
    private static String geoJsonName = "\\长沙市1-V5.geojson";

    private static String geoJsonV1 = "E:\\ownCode\\UMA\\uam\\地图工具-路网";
    private static String geoJsonNameV1 = "\\polylineData1-V4.js";
    public static String urlAll = "E:\\ownCode\\UMA\\数据\\路网数据\\base";


    /**
     * @param flag false 只生成边界数据。 不生成osm文件  true 全量生成
     * @return
     */
    @SneakyThrows
    public static String goGeoJSONV2Local(VMainParam vMainParam, boolean isDelArea) {
        if (vMainParam == null) {
            return null;
        }
        Date sTime = new Date();
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据：" + (DateUtils.formatTime(sTime, new Date())));
        JSONObject jsonObject = goGeoJSONV2(vMainParam.getGeometries(), vMainParam.getGeometry(), isDelArea, Main.floor, urlAll);
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据完成：" + (DateUtils.formatTime(sTime, new Date())));

        JSONArray jsonArray = jsonObject.getJSONArray("features");
        int row = jsonArray.size();
        CountDownLatch countDownLatch = new CountDownLatch(row);
        for (int i = 0; i < row; i++) {
            JSONArray coordinates = jsonArray.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates");
            lonLat6All(countDownLatch, coordinates);
        }
        countDownLatch.await();

        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----转换经纬度数据完成：" + (DateUtils.formatTime(sTime, new Date())));


        sTime = new Date();

        String json = jsonObject.toJSONString();
        WriterFileUtils.writerFoFile(json, geoJson, geoJsonName);
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据完成-geojson：" + (DateUtils.formatTime(sTime, new Date())));
        sTime = new Date();

        WriterFileUtils.writerFoFile("var polylineData1 = " + json, geoJsonV1, geoJsonNameV1);
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据完成polylineData1：" + (DateUtils.formatTime(sTime, new Date())));
        sTime = new Date();
        GeoJsonToOsmXML.jsonToXML(jsonObject, vMainParam.getBounds(), geoOSMUrl, geoOSMName);
        System.out.println(DateUtils.parseDateToStr(new Date()) + "-----全局解析数据完成jsonToXML：" + (DateUtils.formatTime(sTime, new Date())));
        return json;
    }

    private static void lonLat6All(CountDownLatch countDownLatch, JSONArray coordinates)  {
        Main.executorService.execute(new LonLat6Thread(countDownLatch, coordinates));

    }
}
